package zhoug.chart.data;

import android.graphics.drawable.GradientDrawable;

import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import zhoug.chart.core.styleconfig.IMPRadarStyleConfig;
import zhoug.chart.core.styleconfig.MPChartStyleDefaultUtils;
import zhoug.chart.core.styleconfig.MPRadarStyleConfig;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * 雷达图 数据工厂
 *
 * @Author: zhoug
 * @Date: 2024-02-03
 * @Description:
 */
public class MPRadarChartDataCreator {
    private IMPRadarStyleConfig styleConfig;
    private List<String> mXLabels = new ArrayList<>();
    private OnDateSetCreateListener<RadarDataSet> mOnDateSetCreateListener;
    private ValueFormatter mDataSetValueFormatter;

    public MPRadarChartDataCreator() {
        this(new MPRadarStyleConfig());
    }

    public MPRadarChartDataCreator(IMPRadarStyleConfig styleConfig) {
        if (styleConfig != null) {
            this.styleConfig = styleConfig;
        } else {
            this.styleConfig = new MPRadarStyleConfig();
        }
    }

    public void setStyleConfig(@NonNull IMPRadarStyleConfig styleConfig) {
        this.styleConfig = styleConfig;
    }

    public void setDataSetValueFormatter(ValueFormatter dataSetValueFormatter) {
        this.mDataSetValueFormatter = dataSetValueFormatter;
    }

    public void setOnDateSetCreateListener(OnDateSetCreateListener<RadarDataSet> onDateSetCreateListener) {
        this.mOnDateSetCreateListener = onDateSetCreateListener;
    }

    public List<String> getXLabels() {
        return mXLabels;
    }

    /**
     * 创建RadarData
     *
     * @param list
     * @param key
     * @return
     */
    public RadarData crateRadarData(List<HkChartData> list, String key) {
        //x轴名称
        if (mXLabels == null) {
            mXLabels = new ArrayList<>();
        } else {
            mXLabels.clear();
        }
        if (list != null && list.size() > 0) {
            if (key == null) {
                String[] keys = HkChartData.getKeys(list);
                if (keys != null && keys.length > 0) {
                    key = keys[0];
                }
            }
            if (key != null) {
                List<RadarEntry> entries = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    HkChartData hkChartData = list.get(i);
                    mXLabels.add(hkChartData.getName());
                    entries.add(new RadarEntry(hkChartData.getValue(key),  hkChartData));
                }
                RadarDataSet dataSet = new RadarDataSet(entries, "");
                dataSet.setValueFormatter(mDataSetValueFormatter);
                dataSet.setColor(styleConfig.getRadarColor());//
                int[] fillColors = styleConfig.getFillColors();
                if(fillColors!=null && fillColors.length>0){
                    //填充  setFillAlpha(255)并且setDrawFilled(true)则不会绘制数据连接线
                    //使用MRadarChartRenderer渲染器后解决此问题
                    if(fillColors.length==1){
                        dataSet.setFillColor(fillColors[0]);
                    }else{
                        GradientDrawable gradientDrawable = new GradientDrawable();
                        gradientDrawable.setColors(fillColors);
                        gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
                        gradientDrawable.setAlpha(styleConfig.getFillAlpha());
                        dataSet.setFillDrawable(gradientDrawable);
                    }
                    dataSet.setFillAlpha(styleConfig.getFillAlpha());//
                    dataSet.setDrawFilled(true);//
                }else{
                    dataSet.setDrawFilled(false);//
                }
                MPChartStyleDefaultUtils.initRadarDataSet(dataSet);
                if(mOnDateSetCreateListener!=null){
                    mOnDateSetCreateListener.onInitDataSetStyle(dataSet,0);
                }
                RadarData radarData = new RadarData(dataSet);
                return radarData;
            }
        }
        return null;
    }


}
